# _*_ coding:utf-8 _*_
# @Time :2020/11/27 19:56
# @Author :yanxia
from application import db
from application.libs import const
from application.models.models import Bugcalculate
from datetime import datetime, timedelta


def pro_statis(param_data):
    """
    查询最近一周的项目检查点的数据/首轮漏测率、bug密度等
    :return:

    sql ="SELECT vname,proname,versionname,checknum,fristnum,leaknum,newnum,bugcount,bugdensity,fristleak,bringerror,addtime " \
         "FROM bugcalculate " \
         "WHERE YEARWEEK(date_format(addtime,'%Y-%m-%d')) = YEARWEEK(now())"
     """
    NOW = datetime.now()
    page_data = db.session.query(Bugcalculate.vname.label("vname"), Bugcalculate.proname.label("proname"),
                                 Bugcalculate.versionname.label("versionname"), Bugcalculate.checknum.label("checknum"),
                                 Bugcalculate.fristnum.label("fristnum"), Bugcalculate.leaknum.label("leaknum"),
                                 Bugcalculate.newnum.label("newnum"), Bugcalculate.bugcount.label("bugcount"),
                                 Bugcalculate.bugdensity.label("bugdensity"), Bugcalculate.fristleak.label("fristleak"),
                                 Bugcalculate.bringerror.label("bringerror"),
                                 Bugcalculate.addtime.label("addtime")).filter(
        Bugcalculate.addtime >= NOW - timedelta(days=7)).paginate(page=param_data[const.PAGE_NUM],
                                                                  per_page=param_data[const.PAGE_PER], error_out=False)
    project_dic = {}
    data_list = []
    if page_data.items:
        for item in page_data.items:
            project_dic['projectname'] = item.vname + '-' + item.proname + '-' + item.versionname
            project_dic['checknum'] = item.checknum
            project_dic['fristnum'] = item.fristnum
            project_dic['leaknum'] = item.leaknum
            project_dic['newnum'] = item.newnum
            project_dic['bugcount'] = item.bugcount
            project_dic['bugdensity'] = item.bugdensity
            project_dic['fristleak'] = item.fristleak
            project_dic['bringerror'] = item.bringerror
            project_dic['addtime'] = item.addtime
            data_list.append(project_dic.copy())
            project_dic.clear()
    project_dic['items'] = data_list
    project_dic['total'] = page_data.total
    return project_dic
